package com.tjetc.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.dao.AreaMapper;
import com.tjetc.dao.CategoryMapper;
import com.tjetc.dao.DishMapper;
import com.tjetc.dao.MerchantMapper;
import com.tjetc.entity.*;
import com.tjetc.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service

public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 根据id查询商家
     * @param id 商家id
     * @return 返回Merchant对象
     */
    @Override
    public Merchant selectById(int id) {
        Merchant merchant = merchantMapper.selectById(id);
        return merchant;
    }

    /**
     * 通过手机号查询
     * @param phone 传入电话
     * @return 返回商家对象
     */
    @Override
    public Merchant selectByTelephone(String phone) {
        Merchant merchant = merchantMapper.selectByPhone(phone);
        return merchant;
    }

    /**
     * 添加商家
     * @param merchant 传入商家的对象
     * @return 返回前端对象,显示是否添加成功
     */
    @Override
    public Integer addMerchant(Merchant merchant) {
        return merchantMapper.addMerchant(merchant);
    }

    /**
     * 更新商家
     * @param merchant 传入最新的商家对象。根据商家对象中的id查询商家，将最新的值赋值
     * @return 返回前端对象，显示是否完成更新
     */
    @Override
    public Integer updateMerchant(Merchant merchant) {

        return merchantMapper.updateMerchant(merchant);
    }

    /**
     * 更新商家的账户余额
     * @param merchant 商家对象
     * @return 返回是否更新成功
     */
    @Override
    public Integer updateMerchantAmount(Merchant merchant) {
        return merchantMapper.updateMerchantAmount(merchant);
    }

    /**
     * 传入商家的商家名，根据商家名查询商家列表
     * @param shopName 商店名
     * @return 返回商家对象列表
     */
    @Override
    public List<Merchant> selectByShopName(String shopName) {
        List<Merchant> merchants = merchantMapper.selectByShopName(shopName);
        return setMerchantsArea(merchants);
    }


    /**
     * 查询所有的商家，返回商家列表
     * @return 商家列表
     */
    @Override
    public List<Merchant> selectAllMerchant() {
        List<Merchant> merchants =  merchantMapper.selectAllMerchant();
        return merchants;
    }

    /**
     * 查询商家，并且设置动态sql查询和模糊查询
     * @param merchant 查询对象
     * @param pageNum 页面的页数
     * @param pageSize 页面每页的元素数量
     * @return 返回页面封装信息
     */
    @Override
    public PageInfo page(Merchant merchant, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Merchant> merchants = merchantMapper.checkByMerchant(merchant);
        for (Merchant item : merchants) { // 设置名称
            item.setProvinceName(areaMapper.searchAreaById(item.getProvinceId(), 1).getAreaName());
            item.setCityName(areaMapper.searchAreaById(item.getCityId(), 2).getAreaName());
            item.setDistrictName(areaMapper.searchAreaById(item.getDistrictId(), 3).getAreaName());
        }
        return new PageInfo(merchants);
    }

    /**
     * 根据商家的id删除商家
     * @param id 传入的商家id
     * @return 返回是否删除成功
     */
    @Override
    public Integer delete(Integer id) {
        return merchantMapper.removeById(id);
    }


    /**
     * 查询省份
     * @param provinceId 传入areaId
     * @return 返回Area对象
     */
    @Override
    public Area checkProvinceById(Integer provinceId) {
        return areaMapper.searchAreaById(provinceId, 1); // 返回省
    }

    /**
     * 查询城市
     * @param cityId 传入areaId
     * @return 返回Area对象
     */
    @Override
    public Area checkCityById(Integer cityId) {
        return areaMapper.searchAreaById(cityId, 2); // 返回市
    }

    /**
     * 查询区域
     * @param districtId 传入areaId
     * @return 返回Area对象
     */
    @Override
    public Area checkDistrictById(Integer districtId) {
        return areaMapper.searchAreaById(districtId, 3); // 返回区
    }

    /**
     * 根据传入的id数组，批量删除
     * @param ids
     * @return
     */
    @Override
    public Integer deleteFromIds(List<Integer> ids) {
        return merchantMapper.removeByIds(ids);
    }

    /**
     * 查询不同的类型
     * @param typeId
     * @return
     */
    @Override
    public List<Merchant> diffrerntType(Integer typeId) {
        List<Category> ids = categoryMapper.selectBytype(typeId);
        List<Merchant> shops=new ArrayList<>();
        System.out.println(shops.size());
        for(int i=0;i<ids.size();i++){
            List<Dish> dishes = dishMapper.selectByCateId(ids.get(i).getId());
            for(int j=0;j<dishes.size();j++){
                Merchant shop = merchantMapper.selectById(dishes.get(j).getMerchantId());
                if(!shops.contains(shop)){
                    shops.add(shop);
                }
            }
        }
        return shops;
    }

    @Override
    public List<Merchant> shopAndDish(String searchWord) {
        if(searchWord==null){
            return merchantMapper.selectAllMerchant();
        }
        List<Merchant> merchants=new ArrayList<>();
        List<Merchant> merchants1 = merchantMapper.selectByShopName(searchWord);
        if(merchants1!=null){
            for(int i=0;i<merchants1.size();i++){
                merchants.add(merchants1.get(i));
            }
        }
        List<Dish> dishes = dishMapper.selectByDishname(searchWord);
        if(dishes!=null){
            for(int i=0;i<dishes.size();i++){
                Merchant merchant = merchantMapper.selectById(dishes.get(i).getMerchantId());
                if(!merchants.contains(merchant)){
                    merchants.add(merchant);
                }
            }
        }
        return merchants;
    }

    // 批量通过验证
    @Override
    public Integer passMerchants(List<Integer> ids) {
        return merchantMapper.passMerchants(ids);
    }

    // 列出所有的省份
    @Override
    public List<Area> listProvince() {
        return areaMapper.searchAreaByType(1); // 查询所有的省
    }

    // 列出所有的城市
    @Override
    public List<Area> listCities(Integer provinceId) {
        if(provinceId == null) { // 首先需要选择省份
            return null;
        }
        return areaMapper.searchChildAreaById(provinceId);
    }

    /**
     *  查询区 */
    @Override
    public List<Area> listDistrict(Integer cityId) {
        if(cityId == null) {
            return null;
        }
        return areaMapper.searchChildAreaById(cityId);
    }


    /**
     * 内部方法
     * @param merchants 用于设置地址
     * @return
     */

    private List<Merchant> setMerchantsArea(List<Merchant> merchants) {
        for (Merchant item : merchants) { // 设置名称
            item.setProvinceName(areaMapper.searchAreaById(item.getProvinceId(), 1).getAreaName());
            item.setCityName(areaMapper.searchAreaById(item.getCityId(), 2).getAreaName());
            item.setDistrictName(areaMapper.searchAreaById(item.getDistrictId(), 3).getAreaName());
        }
        return merchants;
    }


}
